
<div class="row animate fadeIn">
    <form id="" class="form-horizontal"  method="post">
      <div class="ibox-title" style="border:none;"><h5 style="color: #00afef"><i>Add Meter Reading</i></h5>
        <div class="ibox-tools">    
            <a id = "cancel_add" class="btn btn-sm btn-outline btn-info cancel-btn-tab">Cancel</a>
        </div>
      </div>
        <div class="ibox-content" id="scan">
            <div class="form-group"><label class="col-lg-3 control-label">Mother Meter</label> 
                    <div class="col-lg-5">
                        <select style="" id="" class="chosen-select" name="mm_no">
                          <?php foreach ($mmeters as $mm) { ?>
                          <option value="<?php echo $mm->mmeter_id; ?>" ><?php echo $mm->mother_name; ?></option>
                          <?php } ?>
                        </select>
                    </div>
                    <div class="col-lg-1 input-s">
                        <a id = "getPast_btn" class="btn btn-sm btn-outline btn-info">Proceed</a>
                    </div>
            </div>
            
              
        </div>


        <div class="ibox-content" id="prevdata">
            
            <div class="form-inline form-group" >
              <div>
                  <label class="col-lg-3 control-label">Last Reading Date :</label>
                <div class="col-lg-4">
                  <label id="lrd" class="control-label"> -- </label>
                </div>
              </div>
              <div>
                  <label class="col-lg-3 control-label">Previous Meter Reading :</label>
                <div class="col-lg-2">
                  <label id="pmr" class="control-label"> -- </label>
                </div>
              </div>
            </div>
        </div>


        <div class="ibox-content" id="calculate">
            <div>
                <div class="form-group"><label class="col-lg-3 control-label">New Meter Reading</label> 
                    <div class="col-lg-11 input-s">
                        <input type="text" name="" class="form-control" id="nmr">
                    </div>
                    <div class="col-lg-9 input-s">
                        <a id = "calc_read" class="btn btn-sm btn-outline btn-info">Process</a>
                    </div>
                </div>
            </div>  
        </div>
        <div class="ibox-content" id="details">
            <div>
              <div class="form-inline form-group" >
                <div><label class="col-lg-3 control-label">Period From :</label>
                    <div class="col-lg-4">
                        <label id="period_from" class="control-label"></label>
                    </div>
                </div>
                <div><label class="col-lg-2 control-label">Period To :</label>
                    <div class="col-lg-2">
                          <label id="period_to" class="control-label"></label>
                    </div>
                </div>
              </div>
              <div class="form-inline form-group" >
                  <div><label class="col-lg-3 control-label">Previous :</label>
                      <div class="col-lg-4">
                          <label id="prev_read" class="control-label"> -- </label>
                      </div>
                  </div>
                  <div><label class="col-lg-2 control-label">Present :</label>
                      <div class="col-lg-2">
                          <label id="pres_read" class="control-label"> -- </label>
                      </div>
                  </div>
              </div>
              <div class="form-inline form-group" >
                  <div><label class="col-lg-3 control-label">Consumed :</label>
                      <div class="col-lg-4">
                          <label id="consumed" class="control-label"> -- </label>
                      </div>
                  </div>
              </div>
              <div class="col-lg-7 pull-right">
                    <a id = "submit_read" class="btn btn-sm btn-outline btn-info">Submit Reading</a>
                  </div>
            </div>  
        </div>




            
    </form>
</div>
            
<script>
  $(document).ready(function(){
    $(".chosen-select").chosen({width:"100%;"});
    $('#prevdata , #calculate , #details' ).hide();
    var meter_id =0;
    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth()+1; //January is 0!
    var yyyy = today.getFullYear();

    $('#getPast_btn').click(function(){
        $.ajax({
          type  : "POST",
          url   :"<?php echo base_url($this->session->userdata('forajax') . '/getprevious'); ?>",
          dataType  : 'json',
          data : { "mmeter_id" : $('select[name=mm_no]').val()} ,
          success : function(d){
            if(d == "false"){
              notifymsg("danger",'Already have water bill for this month.');
              
            }else{
              notifymsg("hide",'');


              $('#lrd').html(d[0].prev_date);
              $('#pmr').html(d[0].previous);
              mmeter_id = d[0].mmeter_id;
              

              $('#prevdata , #calculate').show();
              

            }
          },
                complete: function(data) {
                      $('#getPast_btn').removeAttr('disabled');
                },
                beforeSend: function(){
                      $('#getPast_btn').attr('disabled',true);
                      $('#prevdata , #calculate').hide();
                }
        });
      });

    $('#calc_read').click(function(){
        var nmr = $('#nmr').val();
        var consumed = nmr - parseInt($('#pmr').html());

              

        $('#period_from').html($('#lrd').html());
        $('#period_to').html("<?php echo date('d-M-y'); ?>");

        $('#prev_read').html($('#pmr').html());
        $('#pres_read').html(nmr);
        $('#consumed').html(consumed);

        $('#details').show();
    });


    $('#submit_read').click(function(){

      //mmeter_id = d[0].mmeter_id;
      // mm 
      // yyyy
      //$('#pmr').html();
      //$('#pres_read').html();
      //$('#consumed').html();
      //$('#period_to').html();
      $.ajax({
          type  : "POST",
          url   :"<?php echo base_url($this->session->userdata('forajax') . '/save_read'); ?>",
          dataType  : 'json',
          data : {"meter_id":mmeter_id, "rm":mm, "ry":yyyy, "pr":$('#pmr').html(), "cr":$('#pres_read').html() ,"cons":$('#consumed').html() ,"rd":$('#period_to').html() ,"prd" : $('#lrd').html()} ,
          success : function(d){                                                                                                                                                                                                        
           if(d[0].error == 1)
           {
            notifymsg("danger",d[0].msg);
           }else{
            notifymsg("success",'Reading Added Successfully.');
              // $('#cur_calculate').attr('disabled',true);
              // $('#getPast_btn').attr('disabled',true);
              // $('input[name=new_mtr_read]').attr('readonly',true);
              // $('#submit_bill').attr('disabled',true);
              $('#cancel_add').html('Complete');
           }
          },
          beforeSend: function(){
                      $('#calc_read').attr('disabled',true);
                      $('#getPast_btn').attr('disabled',true);
                      $('#submit_read').attr('disabled',true);
          }
        });
    });



    function alert_cancel(){
          swal({
                title: "Are you sure?",
                text: "Your work will not be save",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "Leave",
                cancelButtonText: "Stay",
                closeOnConfirm: true,
                closeOnCancel: true },
            function (isConfirm) {
                if (isConfirm) {
                  swal("success");
                    close_add(); active_search();
                    notifymsg("hide","hide");
                } else {
                    swal("Cancelled");
                }
          });
    }

    $('#addX').click(function(){
      close_add();        
    });

    $('#cancel_add').click(function () {
      if($('#cancel_add').html() == "Complete"){
        close_add(); active_search();
        notifymsg("hide","hide");
      }else{            
        alert_cancel();
      }
    });


  });
       
</script>
